//
//  MYStatusOriginalView.swift
//  MYWeibo
//
//  Created by Magic Yu on 16/4/16.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 8
class MYStatusOriginalView: UIView {
    
    //原创微博的底部约束
    var bottomCons: Constraint?
    var statusViewModel: MYStatusViewModel? {
    
        didSet{
        
            //print(statusViewModel?.status?.retweeted_status?.text)
            //头像
            iconView.sd_setImageWithURL(statusViewModel?.headImageURL)
            //用户名
            nameLabel.text = statusViewModel?.status?.user?.name ?? ""
            //微博等级
            mbRankImageView.image = statusViewModel?.mbImage

            //认证类型
            verifiedImageView.image = statusViewModel?.verifiedImage
                //时间
            timeLabel.text = statusViewModel?.time
            //微博来源
            sourceLabel.text = statusViewModel?.source
            //微博正文
            contentLabel.text = statusViewModel?.status?.text
            
            //配图视图设置数据源
            //卸载约束
            bottomCons?.uninstall()
            if let url = statusViewModel?.status?.pic_urls where url.count > 0 {
            
                //有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(picView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源
                picView.imageURLs = url
                //视图显示
                picView.hidden = false
                
                
            }else {
            
                //没有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源
                picView.imageURLs = nil
                //视图隐藏
                picView.hidden = true
            }
            
        }
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        sepView.backgroundColor = UIColor.orangeColor()
        //圆角的处理
        iconView.cornerRadius = 20
        
        addSubview(picView)
        
        addSubview(sepView)
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(StatusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
        }
        
        //设置原创微博的底部相对于微博正文底部的约束
        self.snp_makeConstraints { (make) -> Void in
            //make.bottom.equalTo(picView.snp_bottom).offset(StatusCellMargin)
            self.bottomCons = make.bottom.equalTo(picView.snp_bottom).offset(StatusCellMargin).constraint
        }

        
    }
    
    /// 配图视图
    private lazy var picView: MYStatusPictureView = MYStatusPictureView()
    
    /// 分隔视图
    private lazy var sepView: UIView = UIView()
    
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 用户等级
    private lazy var mbRankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    /// 用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    /// 用户名
    private lazy var nameLabel: UILabel = UILabel(text: "15期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "15:15", textColor: UIColor.orangeColor(), fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    /// 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我没有男胖呀", textColor: UIColor.darkGrayColor(), fontSize: 15,margin: StatusCellMargin)
    
    
}
